package com.jic.manager.controller.manage.conf;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.PublicnumType;
import com.jic.conf.request.PublicNumTypeRequest;
import com.jic.conf.response.PublicNumTypeResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.PublicnumTypeService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZS on 2020/1/9 11:04
 * 公众号类型管理
 */
@Slf4j
@RestController
@RequestMapping("/publicNumType")
@Api(value = "公众号类型配置", description = "公众号类型配置", tags = "公众号类型配置")
public class PublicNumTypeController {

    @Autowired
    private PublicnumTypeService publicnumTypeService;


    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取公众号类型配置配置信息")
    @ResponseBody
    @RequestMapping(value = "/queryPage")
    public RestResult<PageResult<PublicNumTypeResponse>> queryPage(@RequestBody PublicNumTypeRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        PublicnumType publicnumType = BeanUtil.copy(form, PublicnumType.class);
        PageResult<PublicNumTypeResponse> result = publicnumTypeService.queryPage(publicnumType, page);
        List<PublicNumTypeResponse> publicNumTypeResponseList = result.getRows().stream().map(temp -> {
            PublicNumTypeResponse publicNumTypeResponse = BeanUtil.copy(temp, PublicNumTypeResponse.class);
            return publicNumTypeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<PublicNumTypeResponse> pageResult = new PageResult<>();
        pageResult.setRows(publicNumTypeResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }


    /*
     *  微信公众号类型新增
     *
     * */
    @ResponseBody
    @PostMapping(value = "/editPublicNumType")
    public RestResult insertPublicnumType(@RequestBody PublicNumTypeRequest publicnumTypeRequest) {
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if (publicnumTypeRequest.getId() !=null) {
                publicnumTypeService.update(publicnumTypeRequest);
                restResult = RestResult.success(restResult);
            } else {//新增
                publicnumTypeService.insert(publicnumTypeRequest);
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            log.error("微信公众号类型新增异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;

    }

    /*
     *  微信公众号配置删除
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deletePublicnumType")
    public RestResult deletePublicnumType( @RequestBody  PublicnumType id) {
        RestResult restResult = new RestResult<>();
        try {
            long flag = publicnumTypeService.delete(id);
            if (flag == 1) {
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            log.error("微信公众号配置删除异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;

    }
}
